#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

APP_TARGET_DIR_NAME="chatterbox"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_LFS_SKIP_SMUDGE=1

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/resemble-ai/chatterbox"
export GIT_REPO_BRANCH="master"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.11
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y

echo "lmdprogress:50"


install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    elif current_os_is_windows; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
    fi
}
install_torch

echo "lmdprogress:55"

conda_run_pip_install "numpy>=1.24.0,<1.26.0"
conda_run_pip_install "peft"
# conda_run_pip_install chatterbox-tts

PROJ_TOML_FILE="pyproject.toml"
PROJ_TOML_FILE_BAK="pyproject-bak.toml"
mv $PROJ_TOML_FILE $PROJ_TOML_FILE_BAK

grep -vE 'torch==|torchaudio==|russian-text-stresser' $PROJ_TOML_FILE_BAK > $PROJ_TOML_FILE

conda_run_pip_install -e .

echo "lmdprogress:60"

# export GIT_REPO_URL="${GIT_HOST}/Vuizur/add-stress-to-epub"
# export GIT_REPO_BRANCH="master"
# export LOCAL_REPO_PATH="$CURRENT_APP_PATH/add-stress-to-epub"
# sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"
# conda_run_pip_install "$LOCAL_REPO_PATH"

export PKUSEG_HOME="${CURRENT_APP_PATH}/pkuseg_home"
mkdir -p $PKUSEG_HOME
DOWNLOAD_FILE_NAME="$PKUSEG_HOME/spacy_ontonotes.zip"
download_with_resume $DOWNLOAD_FILE_NAME ${GIT_HOST}/explosion/spacy-pkuseg/releases/download/v0.0.26/spacy_ontonotes.zip


python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_util.py" "$DOWNLOAD_FILE_NAME" "$PKUSEG_HOME/spacy_ontonotes"

echo "lmdprogress:70"

# download models
if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "lmdprogress:80"
conda_run_pip_install huggingface_hub==0.34.2 hf_xet httpx==0.28.1 modelscope
echo "lmdprogress:85"

# model
MODEL_ID_AND_FILES="ResembleAI/chatterbox Cangjie5_TC.json conds.pt grapheme_mtl_merged_expanded_v1.json mtl_tokenizer.json s3gen.pt s3gen.safetensors t3_cfg.safetensors t3_mtl23ls_v2.safetensors tokenizer.json ve.pt ve.safetensors"
if [ -n "$HF_ENDPOINT" ]; then
    export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home_by_modelscope"
    export TRANSFORMERS_CACHE="./tmp_hf_home_by_modelscope/hub"
    export TRANSFORMERS_OFFLINE=1
    export HF_HUB_DISABLE_SYMLINKS=1
    # ResembleAI/chatterbox
    conda_run_hf_dl_by_modelsople "$MODEL_ID_AND_FILES" ResembleAI--chatterbox
else 
    conda_run_hf_download "$MODEL_ID_AND_FILES"
fi

echo "lmdprogress:100"


